using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using NUnit.Framework;
using SCSFContrib.CompositeUI.WPF.Workspaces;

namespace SCSFContrib.CompositeUI.WPF.FunctionalTest.Workspaces
{
	[TestFixture]
	public sealed class ZoneWorkspaceTest : FunctionalTest
	{
		private ZoneWorkspace _zoneWorkspace;

		protected override void SetUpCore()
		{
			base.SetUpCore();
			_zoneWorkspace = new ZoneWorkspace();
		}

		[Test]
		public void OnActivate_ShouldSetFocusOnSpecifiedSmartPart()
		{
			ExecuteInSeparateAppDomain("OnActivate_ShouldSetFocusOnSpecifiedSmartPart_DoWork");
		}

		private void OnActivate_ShouldSetFocusOnSpecifiedSmartPart_DoWork()
		{
			ContentControl defaultZone = new ContentControl();
			ContentControl zone1 = new ContentControl();
			ContentControl zone2 = new ContentControl();
			ContentControl zone3 = new ContentControl();
			string smartPart1 = "abc";
			string smartPart2 = "def";
			string smartPart3 = "ghi";
			string smartPart4 = "jkl";
			StackPanel stackPanel = new StackPanel();
			stackPanel.Children.Add(defaultZone);
			stackPanel.Children.Add(zone1);
			stackPanel.Children.Add(zone2);
			stackPanel.Children.Add(zone3);
			_zoneWorkspace.Content = stackPanel;
			_zoneWorkspace.DefaultZone = defaultZone;
			ZoneWorkspace.SetZoneName(zone1, "zone1");
			ZoneWorkspace.SetZoneName(zone2, "zone2");
			ZoneWorkspace.SetZoneName(zone3, "zone3");

			Window window = CreateWindow(_zoneWorkspace);
			window.Show();

			_zoneWorkspace.Show(smartPart1, new ZoneSmartPartInfo("zone1"));
			_zoneWorkspace.Show(smartPart2, new ZoneSmartPartInfo("zone2"));
			_zoneWorkspace.Show(smartPart3, new ZoneSmartPartInfo("zone3"));
			_zoneWorkspace.Show(smartPart4);

			Assert.IsTrue(defaultZone.IsFocused);
			Assert.IsFalse(zone1.IsFocused);
			Assert.IsFalse(zone2.IsFocused);
			Assert.IsFalse(zone3.IsFocused);

			_zoneWorkspace.Activate(smartPart2);
			Assert.IsFalse(defaultZone.IsFocused);
			Assert.IsFalse(zone1.IsFocused);
			Assert.IsTrue(zone2.IsFocused);
			Assert.IsFalse(zone3.IsFocused);

			_zoneWorkspace.Activate(smartPart3);
			Assert.IsFalse(defaultZone.IsFocused);
			Assert.IsFalse(zone1.IsFocused);
			Assert.IsFalse(zone2.IsFocused);
			Assert.IsTrue(zone3.IsFocused);
		}

		[Test]
		public void FocusingOnSmartPartShouldActivateIt()
		{
			ExecuteInSeparateAppDomain("FocusingOnSmartPartShouldActivateIt_DoWork");
		}

		private void FocusingOnSmartPartShouldActivateIt_DoWork()
		{
			ContentControl defaultZone = new ContentControl();
			ContentControl zone1 = new ContentControl();
			ContentControl zone2 = new ContentControl();
			ContentControl zone3 = new ContentControl();
			string smartPart1 = "abc";
			string smartPart2 = "def";
			string smartPart3 = "ghi";
			string smartPart4 = "jkl";
			StackPanel stackPanel = new StackPanel();
			stackPanel.Children.Add(defaultZone);
			stackPanel.Children.Add(zone1);
			stackPanel.Children.Add(zone2);
			stackPanel.Children.Add(zone3);
			_zoneWorkspace.Content = stackPanel;
			_zoneWorkspace.DefaultZone = defaultZone;
			ZoneWorkspace.SetZoneName(zone1, "zone1");
			ZoneWorkspace.SetZoneName(zone2, "zone2");
			ZoneWorkspace.SetZoneName(zone3, "zone3");

			Window window = CreateWindow(_zoneWorkspace);
			window.Show();

			_zoneWorkspace.Show(smartPart1, new ZoneSmartPartInfo("zone1"));
			_zoneWorkspace.Show(smartPart2, new ZoneSmartPartInfo("zone2"));
			_zoneWorkspace.Show(smartPart3, new ZoneSmartPartInfo("zone3"));
			_zoneWorkspace.Show(smartPart4);

			Assert.IsTrue(defaultZone.IsFocused);
			Assert.AreSame(smartPart4, _zoneWorkspace.ActiveSmartPart);

			zone2.Focus();
			Assert.AreSame(smartPart2, _zoneWorkspace.ActiveSmartPart);

			zone3.Focus();
			Assert.AreSame(smartPart3, _zoneWorkspace.ActiveSmartPart);

			zone1.Focus();
			Assert.AreSame(smartPart1, _zoneWorkspace.ActiveSmartPart);

			defaultZone.Focus();
			Assert.AreSame(smartPart4, _zoneWorkspace.ActiveSmartPart);
		}
	}
}